﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace FRM_Main
{
    public partial class FRM_DoiMK : DevExpress.XtraEditors.XtraForm
    {
        public delegate void SendMessage(string Message);
        public SendMessage Sender;
        public FRM_DoiMK()
        {
            InitializeComponent();
            Sender = new SendMessage(GetMessage);
        }
        private void GetMessage(string Message)
        {
            lblUserName.Text = Message;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FRM_DoiMK_Load(object sender, EventArgs e)
        {
            FRM_DoiMK f = new FRM_DoiMK();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMKCu.Text == "" || txtMKMoi.Text == "" || txtNhapLaiMK.Text == "")
                {
                    MessageBox.Show("Ban chua dien day du thong tin");
                }
                else
                {
                    string cn = "Data Source=GODWAR;Initial Catalog=QuanTriHeThong;User ID=sa;Password=123456";
                    SqlConnection conn = new SqlConnection(cn);
                    string username = lblUserName.Text;
                    string password = txtMKCu.Text;
                    string newPassword = txtMKMoi.Text;
                    string confNewPassword = txtNhapLaiMK.Text;
                    string sqlquery = "Update NGUOIDUNG set MatKhau = @newpassword where TenNguoiDung= @username";

                    SqlCommand cmd = new SqlCommand(sqlquery, conn);
                    cmd.Parameters.AddWithValue("@password", txtMKCu.Text);
                    cmd.Parameters.AddWithValue("@newPassword", txtMKMoi.Text);
                    cmd.Parameters.AddWithValue("@confirmPassword", txtNhapLaiMK.Text);
                    cmd.Parameters.AddWithValue("@username", lblUserName.Text);
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Doi mat khau thanh cong!");
                    conn.Close();

                }
            }
            catch (SqlException)
            {
            }
        }
    }
}